<?php
/**
 * Powered By ThinkPHP 5.0.12
 * Copyright (c) 2017 http://www.bojuwang.net All rights reserved.
 * Created by PhpStorm.
 * Date: 2018/4/27 0027
 * Time: 下午 3:05
 * Author: NanTao <260053530@qq.com>
 */

namespace app\bjwlmanager\controller;
use think\Db;
use think\Controller;
use think\Request;

class Wechat extends Controller
{
    protected $appid;
    protected $secrect;
    protected $accessToken;
    function  __construct()
    {
        $this->appid = sysconf('weixin_appid');
        $this->secrect = sysconf('weixin_appsercet');
        $this->accessToken = $this->getToken($this->appid, $this->secrect);

    }

    /**
     * 发送post请求
     * @param string $url
     * @param string $param
     * @return bool|mixed
     */
    function request_post($url = '', $param = '')
    {
        if (empty($url) || empty($param)) {
            return false;
        }
        $postUrl = $url;
        $curlPost = $param;
        $ch = curl_init(); //初始化curl
        curl_setopt($ch, CURLOPT_URL, $postUrl); //抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch); //运行curl
        curl_close($ch);
        return $data;
    }
    /**
     * 发送get请求
     * @param string $url
     * @return bool|mixed
     */
    function request_get($url = '')
    {
        if (empty($url)) {
            return false;
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 500);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
    /**
     * @param $appid
     * @param $appsecret
     * @return mixed
     * 获取token
     */
    protected function getToken($appid, $appsecret)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
        $token = $this->request_get($url);
        $token = json_decode(stripslashes($token));
        $arr = json_decode(json_encode($token), true);
        $access_token = $arr['access_token'];
        return $access_token;
    }
    /**
     * 发送自定义的模板消息
     * @param $touser
     * @param $template_id
     * @param $url
     * @param $data
     * @param string $topcolor
     * @return bool
     */
    public function doSend($touser, $template_id, $url, $data, $topcolor = '#7B68EE')
    {
        $template = array(
            'touser' => $touser,
            'template_id' => $template_id,
            'url' => $url,
            'topcolor' => $topcolor,
            'data' => $data
        );
        $json_template = json_encode($template);
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $this->accessToken;
        $dataRes = $this->request_post($url, urldecode($json_template));
        if ($dataRes['errcode'] == 0) {
            return true;
        } else {
            return false;
        }
    }



    public function zhongbiao($openid,$usernmae,$goods,$price,$remark="请尽快联系客服，提供相关资料进行提车、过户操作。")
    {
        $data = [
            "touser" => $openid,
            "template_id" => "UMmfhOLk_lyd88FvIF__vBYUzJbQiCUGiMYBqEjuCGo",
            "url" => 'http://weixin.hengdapaimai.com/index.php/mobile/user/paimailist',

            "data" => [
                "first" => [
                    'value' => "恭喜会员：$username 中标。",
                    'color' => '#ff0000',
                ],
                "keyword1" => [
                    'value' => $goods,
                    'color' => '#ff0000',
                ],
                "keyword2" => [
                    'value' => $price,
                    'color' => '#ff0000',
                ],
                "remark" => [
                    'value' => $remark,
                    'color' => '#ff0000',
                ],
            ],
        ];

        return $this->doSend($data);
    }

    public function pushMessage($openid,$title,$pmname,$starttime,$code,$remark="请提前进行竞价登记，做好竞拍准备。")
    {
        if (empty($remark)) {
            $remark="请提前进行竞价登记，做好竞拍准备。";
        }
        $data = [
            "touser" => $openid,
            "template_id" => "iq2J60OwU1mp1L8fxIlDeBMuqVOV4jUugYDjx25tfdY",
            "url" => "http://weixin.hengdapaimai.com/mobile/paimai/info/code/$code",
            "data" => [
                "first" => [
                    'value' => $title,
                    'color' => '#0000cc',
                ],
                "keyword1" => [
                    'value' => $pmname,
                    'color' => '#ff0000',
                ],
                "keyword2" => [
                    'value' => $starttime,
                    'color' => '#ff3333',
                ],
                "remark" => [
                    'value' => $remark,
                    'color' => '#aa5500',
                ],
            ],
        ];

        return $this->doSend($data);
    }
}